package com.starlink.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import com.starlink.model.util.ShoppingCartOrderConvert;
import org.springframework.data.domain.Pageable;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.dtomapper.ShoppingCartOrderDtoMapper;
import com.starlink.model.domain.ShoppingCartOrderDO;
import com.starlink.mapper.ShoppingCartOrderMapper;
import com.starlink.model.request.ShoppingCartOrderRequest;
import com.starlink.service.ShoppingCartOrderService;
import com.starlink.model.vo.ShoppingCartOrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import static com.starlink.utils.PageUtils.getOrderBy;

import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class ShoppingCartOrderServiceImpl extends ServiceImpl<ShoppingCartOrderMapper, ShoppingCartOrderDO> implements ShoppingCartOrderService {
    @Autowired
    private ShoppingCartOrderMapper shoppingCartOrderMapper;

    @Override
    public ShoppingCartOrderVO getByKey(long id) {
        ShoppingCartOrderVO shoppingCartOrderVO = ShoppingCartOrderDtoMapper.INSTANCE.do2VO(getById(id));
        return shoppingCartOrderVO;
    }

    @Override
    public PageInfo<ShoppingCartOrderVO> pageQuery(Pageable pageable,ShoppingCartOrderRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), getOrderBy(pageable.getSort())).setReasonable(true);
        List<ShoppingCartOrderDO> list = shoppingCartOrderMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<ShoppingCartOrderVO> shoppingCartOrderVOList = ShoppingCartOrderConvert.buildShoppingCartOrderVoList(list);
        return new PageInfo(shoppingCartOrderVOList);
    }

    @Override
    public List<ShoppingCartOrderVO> listQuery(ShoppingCartOrderRequest request) {
        List<ShoppingCartOrderDO> list = shoppingCartOrderMapper.selectDOList(ShoppingCartOrderDtoMapper.INSTANCE.request2DO(request));
        List<ShoppingCartOrderVO> shoppingCartOrderVOS = ShoppingCartOrderConvert.buildShoppingCartOrderVoList(list);
        return shoppingCartOrderVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(ShoppingCartOrderRequest request) {
        save(ShoppingCartOrderDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(ShoppingCartOrderRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(ShoppingCartOrderDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDOByIds(String ids) {
        shoppingCartOrderMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insertDO(ShoppingCartOrderRequest request) {
        ShoppingCartOrderDO shoppingCartOrder = ShoppingCartOrderDtoMapper.INSTANCE.request2DO(request);
        shoppingCartOrderMapper.insertDO(shoppingCartOrder);
        return shoppingCartOrder.getId();
    }
}
